package com.cari.oa.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.cari.oa.domain.NotificationEntity;
import com.cari.oa.service.NotificationService;
import com.cari.oa.util.Contants;
import com.cari.oa.util.Utils;

/**
 * 通知控制器
* User: hjx
* Date: 2013-5-8
* Time: 下午4:02:56
 */
@Controller
@SessionAttributes("username")
public class NotificationController {

	@Resource
	private NotificationService notificationService;
	
	public static final String MODULE_NAME = "Notification_MANAGE";
	
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false));
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }
    
    @RequestMapping("/notification/list.do")
    public String list(@ModelAttribute("username") String username, Integer isRead, String title, Integer curPage, ModelMap modelMap) {
        try {
			if(curPage == null) curPage = 1;
			if(isRead == null) isRead = 1;
			long totalRecords = notificationService.getCount(username, isRead, title);
			long totalPage = totalRecords / Contants.PAGE_SIZE + (totalRecords % Contants.PAGE_SIZE != 0 ? 1 : 0);
			
			List<NotificationEntity> notiList = notificationService.getPage(username, isRead, title, curPage, Contants.PAGE_SIZE);

			modelMap.addAttribute("notiList", notiList);
			modelMap.addAttribute("curPage", curPage);
			modelMap.addAttribute("totalPage", totalPage);
			modelMap.addAttribute("totalRecords", totalRecords);
			modelMap.addAttribute("isRead", isRead);
			modelMap.addAttribute("title", title);
		} catch (Exception e) {
			e.printStackTrace();
		}

        return "notification/list";
    }
    
    @RequestMapping(value = "/notification/setRead.do", method = RequestMethod.POST)
    @ResponseBody
    public String edit(@ModelAttribute("username") String username,String id, BindingResult result) {
        NotificationEntity noti = notificationService.getById(id);
        noti.setIsRead(0);
        notificationService.update(noti);
        SysOperLogController.addOperLog(username, MODULE_NAME, "设置已读" + noti.getTitle());
        return Utils.normalJsonResp();
    } 
    
}
